﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace WebApp
{
    public static class WebAppApplicationBuilderExtensions
    {
        public static void UseMyMiddleWare(this IApplicationBuilder app)
        {
            app.Use(next =>
            {
                async Task d(HttpContext context)
                {
                    var str = "aspnet core 1\r\n";
                    var b = Encoding.UTF8.GetBytes(str);
                    context.Items.Add("test", "b");




                    await context.Response.Body.WriteAsync(b, 0, b.Length);
                    await next(context);


                }
                return d;
            });
            app.UseMiddleware<MyMiddleware>();
            app.Use(next =>
            {
                return async (context) =>
                {
                    var str = "aspnet core 2 \r\n";
                    var b = Encoding.UTF8.GetBytes(str);

                    await context.Response.Body.WriteAsync(b, 0, b.Length);
                    //lifeTime.StopApplication();
                };
            });
        }

        public static void UseMyAuthenticationHandler(this IApplicationBuilder app)
        {
            // 登录
            app.Map("/login", builder => builder.Use(next =>
            {
                return async (context) =>
                {
                    var claimIdentity = new ClaimsIdentity();
                    claimIdentity.AddClaim(new Claim(ClaimTypes.Name, "jim"));
                    await context.SignInAsync("myScheme", new ClaimsPrincipal(claimIdentity));
                };
            }));

            // 退出
            app.Map("/logout", builder => builder.Use(next =>
            {
                return async (context) =>
                {
                    await context.SignOutAsync("myScheme");
                };
            }));

            // 认证
            app.Use(next =>
            {
                return async (context) =>
                {
                    var result = await context.AuthenticateAsync("myScheme");
                    if (result?.Principal != null) context.User = result.Principal;
                    await next(context);
                };
            });

            // 授权
            app.Use(async (context, next) =>
            {
                var user = context.User;
                if (user?.Identity?.IsAuthenticated ?? false)
                {
                    if (user.Identity.Name != "zhangsan") await context.ForbidAsync("myScheme");
                    else await next();
                }
                else
                {
                    await context.ChallengeAsync("myScheme");
                }
            });

            // 访问受保护资源
            app.Map("/resource", builder => builder.Run(async (context) => await context.Response.WriteAsync("Hello, ASP.NET Core!")));
        }
    }
}
